package view.Windows;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

public class ErrorMessageGUI {

	final protected Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;

	public ErrorMessageGUI(Display mainDisp, String message) {

		initFonts(mainDisp);

		shlMain = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE & SWT.CLOSE & ~SWT.MAX);
		shlMain.setBounds(10, 10, 300, 200);

		// locate the shell in the center of the display
		Monitor primary = mainDisp.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlMain.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shlMain.setLocation(x, y);

		// main definitions
		shlMain.setText("LegenDate - Error");
		Image icon = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDate.png"));
		shlMain.setImage(icon);
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		Image imgBackground = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDateBG2.jpg"));
		shlMain.setBackgroundImage(imgBackground);
		shlMain.setBackgroundMode(SWT.INHERIT_DEFAULT);

		// the error message
		Label lblErrorMessage = new Label(shlMain, SWT.WRAP);
		lblErrorMessage.setText(message);
		lblErrorMessage.setBounds(20, 40, 260, 50);
		lblErrorMessage.setForeground(clrWhite);
		lblErrorMessage.setFont(getLabelFont());

		// ok button
		Button btnOk = new Button(shlMain, SWT.NONE);
		btnOk.setFont(getButtonFont());
		btnOk.setText("Ok");
		btnOk.setBounds(110, 120, 80, 30);
		btnOk.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				shlMain.close();
				shlMain.dispose();
			}
		});

		// exit popup by pressing 'escape'
		shlMain.addListener(SWT.Traverse, new Listener() {

			public void handleEvent(Event event) {
				switch (event.detail) {
					case SWT.TRAVERSE_ESCAPE:
						shlMain.close();
						shlMain.dispose();
				}
			}
		});

	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	/**
	 * initialize fonts
	 * 
	 * @param display
	 */
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}

	/**
	 * show the shell
	 * 
	 * @param display
	 */
	public void showGUI(Display display) {
		shlMain.open();
		// this is what's holding the shell open!!!
		while (!shlMain.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
